package testview.zhen.com.myapplication.hencoder.practice_draw2

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import testview.zhen.com.myapplication.R

class Practice05ComposeShaderView : View {
    internal var paint = Paint(Paint.ANTI_ALIAS_FLAG)

    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {}

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}

    init {
        setLayerType(View.LAYER_TYPE_SOFTWARE, null) // 硬件加速下 ComposeShader 不能使用两个同类型的 Shader

        var bitmap = BitmapFactory.decodeResource(resources, R.mipmap.batman)
        var bitmap1 = BitmapFactory.decodeResource(resources,R.mipmap.batman_logo)

        var shader = BitmapShader(bitmap,Shader.TileMode.CLAMP,Shader.TileMode.CLAMP)

        var shader1 = BitmapShader(bitmap1,Shader.TileMode.CLAMP,Shader.TileMode.CLAMP)

        var composeShader = ComposeShader(shader,shader1,PorterDuff.Mode.DST_IN)

        paint.shader = composeShader
        // 用 Paint.setShader(shader) 设置一个 ComposeShader
        // Shader 1: BitmapShader 图片：R.drawable.batman
        // Shader 2: BitmapShader 图片：R.drawable.batman_logo
        //好多效果 具体看文档

        //PorterDuff.Mode.SRC_OVER 铺上去效果
        //PorterDuff.Mode.DST_OUT 挖空效果
        //PorterDuff.Mode.DST_IN 蒙版抠图效果
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        canvas.drawCircle(200f, 200f, 200f, paint)
    }
}
